// releasePage/pages/addPackage/addPackage.js
import throttle from '../../../utils/throttle.js';
import Http from '../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        review: '', //审核编辑标记位
        // 滚动区域高度
        ipxBottom: 0,
        // 运营商名称
        businessHallValue: '', //营业厅运营商名称
        businessHallId: '', //营业厅运营商ID
        // 价格
        priceValue: '',
        // 语音（通话时长）
        callValue: '',
        // 数据
        dataValue: '',
        // 短信
        smsValue: '',
        // 彩信
        mmsValue: '',
        // 补充说明内容
        textareaValue: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        if (options.id) {
            wx.setNavigationBarTitle({
                title: '编辑套餐',
            });
            this.getPackageDetails(options.id);
        } else {
            wx.setNavigationBarTitle({
                title: '添加套餐',
            });
        }
        this.setData({
            id: options.id,
            review: options.review || '',
        });
    },

    onInput(e) {
        let item = e.currentTarget.dataset.item;
        this.setData({
            [item]: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 500);
    },

    changeNav: throttle((e) => {
        let nav = e.currentTarget.dataset.nav;
        wx.navigateTo({
            url: nav,
        });
    }, 200),
    // 提交
    submit: throttle(
        function (e) {
            let _that = this;
            if (app.visitorPublish()) return; //游客发布限制
            if (!_that.data.businessHallId) return;
            if (!_that.data.id) {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/createPackage?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        price: _that.data.priceValue,
                        voice: _that.data.callValue,
                        sms: _that.data.smsValue,
                        operatorid: _that.data.businessHallId,
                        effectiveflg: '00',
                        backup: _that.data.textareaValue,
                        mms: _that.data.mmsValue,
                        data: _that.data.dataValue,
                    },
                    success: function (res) {
                        console.log(res);
                        wx.showToast({
                            title: '录入成功',
                            icon: 'success',
                            duration: 2000,
                        });
                        let pages = getCurrentPages(); //当前页面栈
                        let prevPage = pages[pages.length - 2]; //上一页面
                        if (prevPage && typeof prevPage.refresh == 'function') {
                            prevPage.refresh(['package', 'count']);
                        }
                    },
                });
            } else {
                //编辑问题
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/editPackage?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        price: _that.data.priceValue,
                        voice: _that.data.callValue,
                        sms: _that.data.smsValue,
                        operatorid: _that.data.businessHallId,
                        effectiveflg: '00',
                        backup: _that.data.textareaValue,
                        mms: _that.data.mmsValue,
                        data: _that.data.dataValue,
                        id: _that.data.id,
                    },
                    success: function (res) {
                        if (!_that.data.review) {
                            wx.showToast({
                                title: '录入成功',
                                icon: 'success',
                                duration: 2000,
                            });
                            let pages = getCurrentPages(); //当前页面栈
                            let prevPage = pages[pages.length - 2]; //上一页面
                            if (
                                prevPage &&
                                typeof prevPage.refresh == 'function'
                            ) {
                                prevPage.refresh(['package', 'count']);
                            }
                        } else {
                            app.doVerify(res.data.data.id, '18');
                        }
                    },
                });
            }
            if (!_that.data.review) {
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1, // 返回上一级页面。
                    });
                }, 2000);
            }
        },
        4000,
        {
            trailing: false,
        }
    ),

    //获取详情
    getPackageDetails(id) {
        let _that = this;
        http.getRequest(
            `cjMap/getPackageDetails?authId=${app.globalData.authId}&id=${id}`
        ).then((res) => {
            if (res.status == 0) {
                console.log(res);
                _that.setData({
                    businessHallValue: res.data.operator, //营业厅运营商名称
                    businessHallId: res.data.operatorid, //营业厅运营商ID
                    // 价格
                    priceValue: res.data.price,
                    // 语音（通话时长）
                    callValue: res.data.voice,
                    // 数据
                    dataValue: res.data.data,
                    // 短信
                    smsValue: res.data.sms,
                    // 彩信
                    mmsValue: res.data.mms,
                    // 补充说明内容
                    textareaValue: res.data.backup,
                });
            }
        });
    },
});
